package cn.livingCloud.controller.user.system;

import cn.livingCloud.dto.SettingFromDto;
import cn.livingCloud.dto.SystemProfileDto;
import cn.livingCloud.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;

/**
 * Created by DingJie on 2016/1/19.
 */
@Controller
@RequestMapping("/admin/setting")
public class SettingController {

    @Autowired
    private SettingService service;

    @RequestMapping("/load.htm")
    public String load(Model model) {
        SettingFromDto formDto = service.loadFromDto();
        model.addAttribute("formDto", formDto);
        return "user/setting/edit";
    }

    @RequestMapping("/edit.htm")
    public String edit(@ModelAttribute("formDto") @Valid SettingFromDto settingFromDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "user/setting/edit";
        }
        service.update(settingFromDto);
        model.addAttribute("edit", "success");
        return "redirect:/admin/index.htm";
    }

    //系统概要及系统CPU,内存,磁盘等信息
    @RequestMapping("/profile")
    @ResponseBody
    public SystemProfileDto getSystemProfile() {
        return service.getProfile();
    }
}
